<?xml version="1.0" encoding="utf-8"?>
<component>
    <?component error="false" debug="false"?>
    <registration description="upload" progid="component.upload" version="1.00" classid="{f423a37d-6908-4afd-b2c2-0351c3f0ad05}" />
    <public>
        <property name="version" />
        <property name="filter" />
        <property name="files" />
        <method name="save">
            <parameter name="path" />
        </method>
    </public>
    <implements type="ASP" id="ASP"/>
    <script language="JScript" runat="server">
    <![CDATA[

        var version = '1.00';
        var filter = 'rar|zip|txt|gif|jpg|jpeg|bmp|htm|html|swf|mht|doc|xls';
        var files = [];
        
        var _totalBytes;
        var _stream = Server.CreateObject('ADODB.Stream');
        var _position = 0;
        var _buffer = '', _buffers;


        function save(path) {
            var _size = 204800;
            
            _totalBytes = Request.TotalBytes;
            
            if (_size > _totalBytes) _size = _totalBytes;
            
            _stream.open();
            _stream.charset = "unicode";
            while (_position < _totalBytes && Response.IsClientConnected()) {
                _stream.WriteText(Request.BinaryRead(_size));
                _position += _size;
            }

            _stream.Position = 0;
            _buffer = _stream.ReadText();
            
            _stream.Close();
            
            _buffer = _buffer.replace(/[\x00-\xff]/g, function(char) {
                char = char.charCodeAt(0).toString(16).toUpperCase();
                if(char.length < 2) char = '0' + char;
                return '_00' + char;
            });
            
            _buffer = escape(_buffer).replace(/_/g,"%u").replace(/%u(\w{2})(\w{2})/g,"%$2%$1");
            
            var _splitter = _buffer.substr(0, _buffer.indexOf('%0D%0A'));
            
            _buffers = _buffer.substr(0, _buffer.length - 12).split(_splitter);
            
            for (var _index = 0; _index < _buffers.length; _index++) {
                if (_buffers[_index]) {
                    files[_index] = {
                        path:'', 
                        name:'',
                        type:'',
                        size:0,
                        value:'',
                        message:''
                    };
                    var _data = _buffers[_index].split('%0D%0A%0D%0A');
                    _stream.Open();
                    _stream.Charset = "unicode";
                    _stream.WriteText(unescape(_data[0].replace(/%(\w{2})%(\w{2})/g, "%u$2$1")));
                    _stream.Position = 0;
                    _stream.Charset = "latin1";
                    var _info = _stream.ReadText();
                    _stream.Close();

                    if (_info.match(/filename="(.*?)"/)) {
                        files[_index].path = _info.match(/filename="(.*?)"/)[1];
                        files[_index].name = files[_index].path.match(/([^\\]*?)$/)[1];
                        files[_index].type = _info.match(/Type: (.*)$/m)[1];
                        files[_index].value = _data[1].substr(0, _data[1].length - 6);
                        files[_index].size = files[_index].value.length / 3;
                        files[_index].value = unescape(files[_index].value.replace(/%(\w{2})%(\w{2})/g, "%u$2$1"));
                    }
                    else {
                        files.splice(_index, 1);
                    }
                }
            }
            
            if (files.length > 0) files.splice(0, 1);
            
            var _return = [];
            
            for (var _index = 0; _index < files.length; _index++) {
                if (files[_index].name) {
                    var _mark = files[_index].value.charCodeAt(0).toString(16);
                    while(_mark.length < 4) _mark = "0" + _mark;
                    _stream.Type = 2;
                    _stream.Charset = "latin1";
                    _stream.Open();
                    _stream.WriteText(unescape("%" + _mark.slice(2)));
                    _stream.WriteText(unescape("%" + _mark.slice(0, 2)));
                    _stream.Position = 0;
                    _stream.Charset = "unicode";
                    _stream.Position = 2;
                    _stream.WriteText(files[_index].value.slice(1));
                    _stream.Position = 0;
                    _stream.Charset = "latin1";
                    _stream.Position = files[_index].size;
                    _stream.SetEOS();
                    var _fileExt = files[_index].name.match(/\.([^\/\.\\]*)$/);
                
                    if (_fileExt) {
                        if (this.filter.indexOf(_fileExt[1].toLowerCase()) != -1) {
                            var _filename = new Date().toLocaleString().replace(/\D/g, '') + Math.floor(Math.random() * 9 + 1) + (_fileExt ? _fileExt[0] : '');

                            try    {
                                _stream.saveToFile(Server.mapPath(path + _filename), 2);
                                _return.push(path + _filename);
                            }
                            catch(_error) {
                                files[_index].message = _error.message;
                                continue;
                            }
                            finally {
                                _stream.close();
                            }
                        }
                        else {
                            _return.push('disallow');
                        }
                    }
                }
            }
            
            return _return;
        }
    ]]>
    </script>
</component>
